/**
 * 聊天相关API
 */
import { http } from '../utils/http/index'
import type { UploadSignResponse } from '../models/Upload'

/**
 * 同步聊天数据
 * @param roleType 角色类型 1-求职者 2-招聘者
 * @param syncRequest 同步请求参数数组
 * @returns 同步响应数据
 */
export async function syncChatData(roleType: number, syncRequest: Array<{
  sessionId: string
  lastMessageTime: number
}>): Promise<any> {
  try {
    const response = await http.post(`/portal/chat/sync?roleType=${roleType}`, syncRequest)
    return response
  } catch (error) {
    console.error('同步聊天数据失败:', error)
    throw error
  }
}

/**
 * 获取聊天文件上传签名
 * @param showLoading 是否显示loading动画，默认为true
 * @returns MinIO上传签名数据
 */
export async function getChatFileUploadSign(showLoading: boolean = true): Promise<UploadSignResponse> {
  try {
    console.log('[API] 获取聊天文件上传签名')
    const response = await http.get<UploadSignResponse>(
      '/portal/chat/getChatFileUploadSign', 
      {},
      { showLoading }
    )
    console.log('[API] 获取签名成功:', JSON.stringify(response))
    return response
  } catch (error) {
    console.error('[API] 获取上传签名失败:', error)
    throw error
  }
}

/**
 * HR创建或获取与候选人的会话
 * @param candidateId 候选人ID
 * @returns 会话详情
 */
export async function createHrSession(candidateId: string): Promise<any> {
  try {
    console.log('[API] HR创建会话，candidateId:', candidateId)
    const response = await http.post(`/portal/chat/session/add/hr?candidateId=${candidateId}`)
    console.log('[API] 会话创建成功:', JSON.stringify(response))
    return response
  } catch (error) {
    console.error('[API] 创建会话失败:', error)
    throw error
  }
}


